﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Telerik.Web.UI;
using Acms.Bll;
using Acms.AcmsControls.ContentControls.Admin;

public partial class Posts_Controls : BasePostPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dlControls.ItemCommand += new EventHandler<Acms.WebControls.AcmsDataListItemCommandEventArgs>(dlControls_ItemCommand);
        if (!IsPostBack)
        {
        }
    }

    void dlControls_ItemCommand(object sender, Acms.WebControls.AcmsDataListItemCommandEventArgs e)
    {
        if (e.CommandName == "Clear")
        {
            int postId = (int)dlControls.DataKeys[e.Item.ItemIndex].Values["PostId"];
            int templateControlId = (int)dlControls.DataKeys[e.Item.ItemIndex].Values["TemplateControlId"];
            object concurencyId = dlControls.DataKeys[e.Item.ItemIndex].Values["ConcurencyId"];
            PostControl ctrl = PostControl.GetByPostIdAndTemplateControlId(postId, templateControlId);
            if (ctrl != null)
            {
                ctrl.CheckConcurent(concurencyId);
                ctrl.Delete();
                dlControls.DataBind();
            }
        }
    }

    private Acms.Bll.Role.Roles[] EditRoles = new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentPublisher };

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentPublisher }; }
    }

    protected void dsPostControls_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.PostControl> e)
    {
        if (CurrentPost != null)
        {
            e.SetData(CurrentPost.Controls);
        }
    }

    protected void dlControls_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsDataItem)
        {
            PostControl postControl = e.Item.DataItem as Acms.Bll.PostControl;
            if (postControl != null)
            {
                AdminContentControlLoader loader = e.Item.FindControl("adminCC") as AdminContentControlLoader;
                if (loader != null)
                {
                    loader.PostControl = postControl;
                    loader.Post = CurrentPost;
                    loader.DataBind();
                }
                LinkButton btnEdit = e.Item.FindControl("btnEdit") as LinkButton;
                if (btnEdit != null)
                {
                    string url = ResolveUrl(string.Format("PostControlPopUp.aspx?pid={0}&tcid={1}&aid={2}&sid={3}&svid={4}", postControl.PostId, postControl.TemplateControlId, e.Item.UniqueID, AcmsContext.SiteId, Server.UrlEncode(SelectedVersion.ToString())));
                    btnEdit.OnClientClick = string.Format("javascript:OpenEditor('{0}', 800, 600);", url);
                }
            }
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Content");
    }

}
